package cn.jingzhuan.element.demo.view.util

object UnitParseUtils {

  const val EMPTY_VALUE = "--"

  @JvmStatic
  fun parseValue(value: Float, savePoint: Int = 2): String {
    if (value.isNaN()) return EMPTY_VALUE
    return when {
      (Math.abs(value) > 1000000000f && savePoint == 0) -> String.format("%${getFormat(savePoint)}亿", value / 100000000f)
      (Math.abs(value) > 100000000f && savePoint > 0) -> String.format("%${getFormat(savePoint)}亿", value /  100000000f)
      (Math.abs(value) > 10000f && savePoint == 0) -> String.format("%${getFormat(savePoint)}万", value / 10000f)
      (Math.abs(value) > 100000f && savePoint > 0) -> String.format("%${getFormat(savePoint)}万", value / 10000f)
      else -> String.format("%${getFormat(savePoint)}", value)
    }
  }

  @JvmStatic
  fun parseNumber(value:Int, savePoint: Int = 2):String {
    return when {
      (Math.abs(value) > 100000000f) -> String.format("%${getFormat(savePoint)}亿", value /  100000000f)
      (Math.abs(value) > 10000f) -> String.format("%${getFormat(savePoint)}万", value / 10000f)
      else -> value.toString()
    }
  }

  @JvmStatic
  fun getFormat(savePoint: Int) = ".${savePoint}f"


}
